﻿Imports System.Security.Cryptography
Imports System.Text

Public Class ConvertHelper
    Public Shared Function ToDateTime(ByVal UNIX_TIMESTAMP As Long) As DateTime
        Dim dt As New DateTime(1900, 1, 1, 0, 0, 0)
        Return dt.AddSeconds(UNIX_TIMESTAMP)
    End Function

    Public Shared Function ToDateTime(ByVal UNIX_TIMESTAMP As Long, ByVal FORMAT As String) As String
        Return ToDateTime(UNIX_TIMESTAMP).ToString(FORMAT)
    End Function

    Public Shared Function ToUnixTimestamp(ByVal TIME As DateTime) As Long
        Dim timeSpan As TimeSpan = TIME.Subtract(New DateTime(1900, 1, 1, 0, 0, 0).ToLocalTime())
        Return Convert.ToInt64(timeSpan.TotalSeconds())
    End Function

    Public Shared Function ToMD5(ByVal INPUT As String)
        Dim md5haser = MD5.Create()
        Dim data = md5haser.ComputeHash(Encoding.Default.GetBytes(INPUT))
        Dim sBuilder As New StringBuilder()

        For i As Integer = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next
        Return sBuilder.ToString()
    End Function

    Public Shared Function VerifyMD5(ByVal INPUT As String, ByVal HASH As String)
        Dim hashOfInput As String = ToMD5(INPUT)
        Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase
        If comparer.Compare(hashOfInput, HASH) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Shared Function GenerateAccount(ByVal PREFIX As String)
        Dim r As New Random()
        Return PREFIX & r.Next(1000000, 9999999)
    End Function
End Class
